using System;
using System.IO;
using System.Text;

namespace ThalesHsmComms
{
	public class TranslateZpkZmk2LmkMsg : WSMMsg
	{

		public String DestZpk{get;private set;}
		public String CheckDigits{get;private set;}
		
		private String zpk;
		private String zmk;

		public TranslateZpkZmk2LmkMsg(String zpk, String zmk, char zmkScheme = 'U')
			:base("FA")
		{
			this.zmk = zmkScheme + zmk;
			this.zpk = "X" + zpk;
		}

		override protected void Encode() 
		{
	
			StringBuilder data = new StringBuilder();
			
			try {
			
				// pack command code
				data.Append(this.commandCode);
			
				// pack zmk
				data.Append(this.zmk);
				
				// pack zpk to translate
				data.Append(this.zpk);		
				
				this.requestData = ASCIIEncoding.ASCII.GetBytes(data.ToString());
			
			} catch (IOException ex) {
				Console.WriteLine(ex);
			}
		}

		override protected void Decode() 
		{
			// get zpk under lmk
			this.GetNextBytesFromResponse(1);
			this.DestZpk = this.GetNextBytesFromResponse(32);
			
			// get check digits
			this.CheckDigits = this.GetNextBytesFromResponse(6);
			
		}

		override public String ToString() {
			
			StringBuilder str = new StringBuilder();
			
			str.Append("Request\n");
			str.Append("=======\n");
			str.Append("Raw data: " + this.requestData + "\n");
			str.Append("Command code: " + this.commandCode + "\n");
			str.Append("ZPK: " + this.zpk + "\n");
			str.Append("ZMK: " + this.zmk + "\n");
			
			str.Append("\nResponse\n");
			str.Append("========\n");
			str.Append("Raw data: " + this.responseData + "\n");
			str.Append("Response code: " + this.responseCode + "\n");
			str.Append("Error code: " + this.errorCode + "\n");
			str.Append("Destination ZPK: " + this.DestZpk + "\n");
			str.Append("Check Digit: " + this.CheckDigits + "\n");
			
			return str.ToString();
		}

	}
}

